package com.adopt.controller;

import com.adopt.pojo.*;
import com.adopt.service.CommentService;
import com.adopt.util.Message;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhouyue
 * @Date: 2023/09/12/10:13
 * @Description:
 */

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/toComment")
    public String toComment(Model model){

        List<Comment> comments=commentService.getComments();

        model.addAttribute("comments",comments);

        return "admin/comment";
    }

    @RequestMapping("/comments")
    @ResponseBody
    public Message getComments(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        PageHelper.startPage(pn,4);
        List<Comment> comments = commentService.getComments();
        System.out.println(comments);
        PageInfo page=new PageInfo(comments,2);
        return Message.success().add("pageInfo",page);
    }

    @RequestMapping("/comment")
    @ResponseBody
    public Message getComment(){
        List<Comment> comments = commentService.getComments();
        if(comments!=null){
            return Message.success().add("comment",comments);
        }else{
            return Message.fail();
        }

    }

    @RequestMapping("/petComments")
    @ResponseBody
    public Message getPetComment(Integer pet_id){
        List<Comment> comments = commentService.findByPetId(pet_id);
        for(Comment c:comments){
            System.out.println(c);
        }
        if(comments!=null){
            return Message.success().add("comment",comments);
        }else{
            return Message.fail();
        }

    }

    @RequestMapping("/create")
    @ResponseBody
    public Message createComment(String content, HttpServletRequest request){
        Comment comment=new Comment();
        Users user = (Users)request.getSession().getAttribute("user");
        Pet pet =(Pet) request.getSession().getAttribute("pet");
        comment.setUser(user);
        comment.setPet(pet);
        comment.setContent(content);
        comment.setCommentTime(new Date());
        if(commentService.addComment(comment)>0){
            return Message.success();
        }else{
            return Message.fail();
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String deleteComment(Integer id, Model model){
        Result result=commentService.deleteComment(id);

        if(result.getCode()==100){

            return toComment(model);
        }
        return null;
    }


    @RequestMapping("/update")
    @ResponseBody
    public String updateComment(Comment comment, Model model){

        Result result=commentService.updateComment(comment);

        if(result.getCode()==100){
            return toComment(model);

        }else {
            model.addAttribute("msg",result.getMsg());
            return  "comment";
        }
    }



    @RequestMapping("/findById")
    @ResponseBody
    public Message findById(Integer id){
        Comment comment1 = commentService.findById(id);
        if(comment1!=null){
            return Message.success().add("comment",comment1);
        }else{
            return Message.fail();
        }
    }


    @RequestMapping("/findByName")
    @ResponseBody
    public Message findByName(String name){
        PageHelper.startPage(1,4);
        List<Comment> comments = commentService.findByName(name);
        if(comments!=null){
            PageInfo page=new PageInfo(comments,3);
            return Message.success().add("pageInfo",page);
        }else{
            return Message.fail();
        }
    }
}
